package com.sky.controller.client;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.domain.Blog;
import com.sky.domain.Type;
import com.sky.service.BlogService;
import com.sky.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author 尹稳健~
 * @version 1.0
 */
@Controller
public class TypeShowController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @GetMapping("/types/{id}")
    public String type(@PathVariable("id") Long id, Model model,
                       @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){
        List<Type> types = typeService.getBlogType();
        PageHelper.startPage(pageNum,5);
        //-1从导航点过来的
        if (types != null && types.size() > 0) {
            if (id == -1) {
                id = types.get(0).getId();
            }
        }
        List<Blog> blogList = blogService.getByTypeId(id);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogList);
        model.addAttribute("types", types);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("activeTypeId", id);
        return "types";
    }

}
